package com.cubeofnine.utils;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.MarshalFloat;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class WebServiceAdapter {

	private String url;
	private String nameSpace;

	private int timeOutMsec = 10;

	private boolean dotNet;

	private HttpTransportSE httpTransport = null;
	private SoapSerializationEnvelope envelope = null;

	private String operation;

	private SoapObject request = null;
	// private Object response = null;

	private String requestDump = null;
	private String responseDump = null;

	public WebServiceAdapter(String url, String nameSpace, int timeOutMsec,
			boolean isDotNet, String encodingStyle) {
		setUrl(url);
		setNameSpace(nameSpace);
		setTimeOutMsec(timeOutMsec);
		setDotNet(isDotNet);
		httpTransport = new HttpTransportSE(getUrl());
		httpTransport.debug = true;

		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = isDotNet();
		if (encodingStyle != null && !encodingStyle.isEmpty()) {
			envelope.encodingStyle = encodingStyle;
		}

		Marshal floatMarshal = new MarshalFloat();
		floatMarshal.register(envelope);
	}

	public void intiRequest(String operation) {
		setOperation(operation);
		request = new SoapObject(getNameSpace(), operation);
	}

	public void setRequestProperty(String name, Object value) {
		if (value != null) {
			setRequestProperty(name, value, value.getClass());
		} else {
			request.addProperty(name, value);
		}
	}

	public void setRequestProperty(String name, Object value, Class<?> classType) {
		PropertyInfo propertyInfo = new PropertyInfo();
		propertyInfo.setName(name);
		propertyInfo.setValue(value);
		propertyInfo.setType(classType);
		propertyInfo.setNamespace(getNameSpace());
		setRequestProperty(propertyInfo);
	}

	public void setRequestProperty(PropertyInfo propertyInfo) {
		request.addProperty(propertyInfo);
	}

	public void call() throws Exception {
		try {
			envelope.setOutputSoapObject(request);
			httpTransport.call(getSoapAction(getOperation()), envelope);
		} catch (Exception e) {
			throw e;
		} finally {
			this.requestDump = httpTransport.requestDump;
			this.responseDump = httpTransport.responseDump;
		}
	}

	public Object callService() throws Exception {
		call();
		return getResponse();
	}

	public String getRequest() {
		return String.valueOf(request);
	}

	public Object getResponse() throws Exception {
		return envelope.getResponse();
	}

	public String getSoapAction(String operation) {
		return getNameSpace() + "/" + operation + "/";
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getNameSpace() {
		return nameSpace;
	}

	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	public int getTimeOutMsec() {
		return timeOutMsec;
	}

	public void setTimeOutMsec(int timeOutMsec) {
		this.timeOutMsec = timeOutMsec;
	}

	public boolean isDotNet() {
		return dotNet;
	}

	public void setDotNet(boolean dotNet) {
		this.dotNet = dotNet;
	}

	public String getRequestDump() {
		return requestDump;
	}

	public String getResponseDump() {
		return responseDump;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

}
